package com.to.aboomy.bannersample.slice;

import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.fragment.Test1Fragment;
import com.to.aboomy.bannersample.fragment.Test2Fragment;
import com.to.aboomy.bannersample.fragment.Test3Fragment;
import com.to.aboomy.bannersample.util.Utils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class FragmentViewPagerAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final int MCURRENTPOSITION = -1;
    private Test1Fragment test1Fraction;
    private Test2Fragment test2Fraction;
    private Test3Fragment test3Fraction;
    private StackLayout fractionLayout;
    private Text test1,test2,test3;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_fragment_view_pager);
        fractionLayout = (StackLayout) findComponentById(ResourceTable.Id_fractionlayout);

        test1 = (Text) findComponentById(ResourceTable.Id_test1);
        test2 = (Text) findComponentById(ResourceTable.Id_test2);
        test3 = (Text) findComponentById(ResourceTable.Id_test3);

        test1.setClickedListener(this);
        test2.setClickedListener(this);
        test3.setClickedListener(this);

        // 区别是viewPage 跳转还是viewPage2 跳转
        String key = intent.getStringParam(Utils.PARAMETER_KEY);
        int pageIndex = 0;
        boolean isPageIndex = true;
        if(key != null){
            pageIndex = 1;
            isPageIndex = true;
        }
        test1.setTextColor(new Color(Color.getIntColor(isPageIndex ? "#77787b" : "#FFFA0303")));
        test2.setTextColor(new Color(Color.getIntColor(isPageIndex ? "#FFFA0303": "#77787b")));
        selectFraction(pageIndex);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_test1:
                selectFraction(0);
                break;

            case ResourceTable.Id_test2:
                selectFraction(1);
                break;

            case ResourceTable.Id_test3:
                selectFraction(2);
                break;
        }
    }

    /**
     * selectFraction
     *
     * @param index
     */
    private void selectFraction(int index) {
        if (index == MCURRENTPOSITION) {
            return;
        }

        FractionScheduler fractionScheduler = getFractionManager().startFractionScheduler();
        hideAllFraction(fractionScheduler);

        switch (index) {
            case 0:
                if (test1Fraction == null) {
                    test1Fraction = new Test1Fragment();
                    fractionScheduler.add(fractionLayout.getId(), test1Fraction);
                } else {
                    fractionScheduler.show(test1Fraction);
                }
                test1.setTextColor(Color.RED);
                test2.setTextColor(Color.GRAY);
                test3.setTextColor(Color.GRAY);
                break;
            case 1:
                if (test2Fraction == null) {
                    test2Fraction = new Test2Fragment();
                    fractionScheduler.add(fractionLayout.getId(), test2Fraction);
                } else {
                    fractionScheduler.show(test2Fraction);
                }
                test1.setTextColor(Color.GRAY);
                test2.setTextColor(Color.RED);
                test3.setTextColor(Color.GRAY);
                break;
            case 2:
                if (test3Fraction == null) {
                    test3Fraction = new Test3Fragment();
                    fractionScheduler.add(fractionLayout.getId(), test3Fraction);
                } else {
                    fractionScheduler.show(test3Fraction);
                }
                test1.setTextColor(Color.GRAY);
                test2.setTextColor(Color.GRAY);
                test3.setTextColor(Color.RED);
                break;
            default:
                break;
        }
        fractionScheduler.submit();
    }

    /**
     * getFractionManager
     *
     * @return FractionManager
     */
    private FractionManager getFractionManager() {
        FractionManager fractionManager = null;
        Ability ability = getAbility();
        if (ability instanceof FractionAbility) {
            FractionAbility fractionAbility = (FractionAbility) ability;
            fractionManager = fractionAbility.getFractionManager();
            return fractionManager;
        }
        return fractionManager;
    }

    /**
     * hideAllFraction
     *
     * @param fractionScheduler
     */
    private void hideAllFraction(FractionScheduler fractionScheduler) {
        if (test1Fraction != null) {
            fractionScheduler.hide(test1Fraction);
        }
        if (test2Fraction != null) {
            fractionScheduler.hide(test2Fraction);
        }
        if (test3Fraction != null) {
            fractionScheduler.hide(test3Fraction);
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();



    }
}
